#include "usr_include.h"
#define READ_BUFFER_SIZE  100*1024
#define I2S_BLOCK_PLAY_DELAY 3000
static BYTE ReadBuffer0[READ_BUFFER_SIZE] = {0}; /* 读缓冲区 */
static BYTE WriteBuffer[1024];                   /* 写缓冲区*/
osSemaphoreId_t i2s_semaphore;
void bsp_i2s_task(void)
{
    FATFS fs;       /* FatFs文件系统对象 */
    FIL fnew;       /* 文件对象 */
    FRESULT res_sd; /* 文件操作结果 */
    UINT fnum;      /* 文件成功读写数量 */
    uint32_t read_count = 0;
    uint32_t write_count = 0;
    uint8_t state = 0;
    uint32_t song_size = 0x0279f000;
	uint32_t temp2 = 0;
loop:
    res_sd = f_mount(&fs, "0:", 1);

    if (res_sd != FR_OK)
    {
        printf("SD卡挂载文件系统失败\n");
        goto loop;
    }
    else
    {
        printf("文件系统挂载成功，可以进行读写测试\n");
    }
    i2s_semaphore = osSemaphoreNew(1U, 0U, NULL);
    while (1)
    {
        osDelay(1);
        switch (state)
        {
        case (0):
        {
            res_sd = f_open(&fnew, "0:song1.wav", FA_READ);
            if (res_sd != FR_OK)
            {
                f_close(&fnew);
                break;
            }
            read_count = 0;
            res_sd = f_read(&fnew, ReadBuffer0, 0x4e, &fnum); // 读取文件头
            if (res_sd != FR_OK)
            {
                f_close(&fnew);
                break;
            }
            song_size=*((uint32_t *)(&ReadBuffer0[74]));
            res_sd = f_read(&fnew, ReadBuffer0, READ_BUFFER_SIZE, &fnum);
            read_count += READ_BUFFER_SIZE;
            HAL_SAI_Transmit_DMA(&hsai_BlockA1, ReadBuffer0, READ_BUFFER_SIZE/2);
            state = 1;
        }
        break;
        case (1):
        {
					osSemaphoreAcquire(i2s_semaphore, I2S_BLOCK_PLAY_DELAY);//等待上半部播放完成
            if (read_count <= song_size - READ_BUFFER_SIZE/2)
            {
                res_sd = f_read(&fnew, &ReadBuffer0[0], READ_BUFFER_SIZE/2, &fnum);
                read_count += READ_BUFFER_SIZE/2;
                state = 2;
            }
            else
            {
                temp2 = song_size - read_count;
                memset(&ReadBuffer0[0],0,READ_BUFFER_SIZE/2);
                res_sd = f_read(&fnew, &ReadBuffer0[0], temp2, &fnum);
                osSemaphoreAcquire(i2s_semaphore, I2S_BLOCK_PLAY_DELAY);
                osSemaphoreAcquire(i2s_semaphore, I2S_BLOCK_PLAY_DELAY);
                HAL_SAI_DMAStop(&hsai_BlockA1);
                state = 3;
            }
        }
        break;
        case (2):
        {
            osSemaphoreAcquire(i2s_semaphore, I2S_BLOCK_PLAY_DELAY);//等待下半部播放完成
            if (read_count <= song_size - READ_BUFFER_SIZE/2)
            {
                res_sd = f_read(&fnew, &ReadBuffer0[READ_BUFFER_SIZE/2], READ_BUFFER_SIZE/2, &fnum);
                read_count += READ_BUFFER_SIZE/2;
                state = 1;
            }
            else
            {
                temp2 = song_size - read_count;
                memset(&ReadBuffer0[READ_BUFFER_SIZE/2],0,READ_BUFFER_SIZE/2);
                res_sd = f_read(&fnew, &ReadBuffer0[READ_BUFFER_SIZE/2], temp2, &fnum);
                osSemaphoreAcquire(i2s_semaphore, I2S_BLOCK_PLAY_DELAY);
                osSemaphoreAcquire(i2s_semaphore, I2S_BLOCK_PLAY_DELAY);
                HAL_SAI_DMAStop(&hsai_BlockA1);
                state = 3;
            }
        }
        break;
        case (3):
        {
            fclose(&fnew);
            state=0;
            printf("play over");
        }
        break;
        default:
        {
            state = 0;
        }
        break;
        }
    }
    /* 不再使用文件系统，取消挂载文件系统 */
    // f_mount(NULL, "0:", 1);
}